﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Data;

namespace ipi.MCC.Controls
{
    public class MCCDocumentViewControl : UserControl
    {
        public MCCDocumentViewControl() : base() { }

        public void SetSelectedNode(int iSelectedNode)
        {
            if (gvDocuments != null)
            {
                gvDocuments.SetSelectedNode(iSelectedNode);
            }

            if (gvItems != null)
            {
                gvItems.SetSelectedNode(iSelectedNode);
            }
        }

        public int PagingCount { get; set; }
        public bool EnablePaging { get; set; }

        protected UpdatePanel upMain;
        protected CustomValidator validGlobal;
        protected MCCItemsViewGrid gvItems;
        protected MCCDocumentViewGrid gvDocuments;
        protected System.Web.UI.WebControls.Table tblMain;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.Controls.Clear();
            upMain = new UpdatePanel();            

            validGlobal = new CustomValidator();
            validGlobal.Display = ValidatorDisplay.Dynamic;
            upMain.ContentTemplateContainer.Controls.Add(validGlobal);

            try
            {
                tblMain = new Table();
                tblMain.Width = new Unit(100, UnitType.Percentage);
                tblMain.BorderWidth = new Unit(0);

                TableRow trDocumentLabel = new TableRow();
                TableCell tcDocumentLabel = new TableCell();
                Label lblDocument = new Label();
                lblDocument.Text = Resources.Resources.MCCDocumentView_lblDocument;
                lblDocument.Font.Underline = true;
                lblDocument.Font.Bold = true;
                tcDocumentLabel.Controls.Add(lblDocument);
                trDocumentLabel.Cells.Add(tcDocumentLabel);
                tblMain.Rows.Add(trDocumentLabel);

                TableRow trDocumentGrid = new TableRow();
                TableCell tcDocumentGrid = new TableCell();
                gvDocuments = new MCCDocumentViewGrid();
                gvDocuments.PagingCount = this.PagingCount;
                gvDocuments.EnablePaging = this.EnablePaging;

                tcDocumentGrid.Controls.Add(gvDocuments);
                trDocumentGrid.Cells.Add(tcDocumentGrid);
                tblMain.Rows.Add(trDocumentGrid);
                
                TableRow trItemLabel = new TableRow();
                TableCell tcItemLabel = new TableCell();
                Label lblItem = new Label();
                lblItem.Text = Resources.Resources.MCCDocumentView_lblItem;
                lblItem.Font.Underline = true;
                lblItem.Font.Bold = true;
                tcItemLabel.Controls.Add(lblItem);
                trItemLabel.Cells.Add(tcItemLabel);
                tblMain.Rows.Add(trItemLabel);                
                
                TableRow trItemGrid = new TableRow();
                TableCell tcItemGrid = new TableCell();
                gvItems = new MCCItemsViewGrid();
                gvItems.PagingCount = this.PagingCount;
                gvItems.EnablePaging = this.EnablePaging;

                tcItemGrid.Controls.Add(gvItems);
                trItemGrid.Cells.Add(tcItemGrid);
                tblMain.Rows.Add(trItemGrid);              

                upMain.ContentTemplateContainer.Controls.Add(tblMain);
            }
            catch (Exception exc)
            {
                validGlobal.ErrorMessage = exc.Message;
                validGlobal.IsValid = false;
            }

            this.Controls.Add(upMain);
        }
    }
}
